const  mysql = require('mysql');
const connection= require('./Connection');

// 登录主人
const loginUser = function (res,user,pwd){
    console.log(user,pwd)
    const sql = `select * from petowner where name =? and password=? `;
    const params=[user,pwd];
    connection.query(sql,params,function (err,result) {
        if (err){
            console.log(err);
            return;
        }else{
            if (result.length==1){
                let s =  `{"data":[`
                for (const item of result) {
                    s+=`{`
                    for (const key in item) {
                        s+=`"${key}":"${item[key]}",`
                    }
                    s = s.substring(0,s.length-1);
                    s+=`},`
                }
                s = s.substring(0,s.length-1);
                s+=`]}`
                res.send(s);
            }else{
                res.send('err')
            }
        }
    })
}

// 登录商店
const loginStore = function (res,user,pwd){
    console.log(user,pwd)
    const sql = `select * from petstore where name =? and password=? `;
    const params=[user,pwd];
    connection.query(sql,params,function (err,result) {
        if (err){
            console.log(err);
            return;
        }else{
            console.log(result)
            if (result.length==1){
                let s =  `{"data":[`
                for (const item of result) {
                    s+=`{`
                    for (const key in item) {
                        s+=`"${key}":"${item[key]}",`
                    }
                    s = s.substring(0,s.length-1);
                    s+=`},`
                }
                s = s.substring(0,s.length-1);
                s+=`]}`
                res.send(s);
            }else{
                res.send('err')
            }
        }
    })
}
// 购买信息
const selectPet = function (res){
    let sql = `select * from pet where owner_id = 0 and store_id<>0`;
    connection.query(sql,function (err,result) {
        if (err){
            console.log(err);
            return;
        }else{
            let s =  `{"count":"${result.length}","data":[`
            for (const item of result) {
                s+=`{`
                for (const key in item) {
                    s+=`"${key}":"${item[key]}",`
                }
                s = s.substring(0,s.length-1);
                s+=`},`
            }
            s = s.substring(0,s.length-1);
            s+=`]}`
            res.send(s);
        }
    })
}
// 主任拥有的宠物/出售
const selectOwner = function (res,ownerid){
    let sql = `select * from pet where owner_id = ?`;
    let params=[ownerid];
    connection.query(sql,params,function (err,result) {
        if (err){
            console.log(err);
            return;
        }else{
            let s =  `{"count":"${result.length}","data":[`
            for (const item of result) {
                s+=`{`
                for (const key in item) {
                    s+=`"${key}":"${item[key]}",`
                }
                s = s.substring(0,s.length-1);
                s+=`},`
            }
            s = s.substring(0,s.length-1);
            s+=`]}`
            res.send(s);
        }
    })
}
// 主人余额
const updatePetOwner = function (ownerid,type){
    let sql='';
    if (type == 1){
        sql = `update petowner set money = money - 500 where id = ? `;
    }else{
        sql = `update petowner set money = money + 500 where id = ? `;
    }
    let params = [ownerid];
    connection.query(sql,params,function (err,result) {
        if (err){
            console.log(err);
            return
        }else{
            console.log(result)
        }
    })

}
//商店余额
const updatePetStore = function (storeid,type){
    let sql='';
    if (type == 1){
        sql = `update petstore set balance = balance - 500 where id = ? `;
    }else{
        sql = `update petstore set balance = balance + 500 where id = ? `;
    }
    let params = [storeid];
    connection.query(sql,params,function (err,result) {
        if (err){
            console.log(err);
            return
        }else{
            console.log(result)
        }
    })

}
// 显示宠物  主人买
const ownerBuy = function (ownerid,storeid,petid){
    let sql = `update pet set store_id = 0 , owner_id = ? where id = ? and store_id=?`;
    let params = [ownerid,petid,storeid];
    connection.query(sql,params,function (err,result) {
        if (err){
            console.log(err);
            return
        }else{
            console.log(result)
        }
    })
}
// 显示宠物  主人卖
const ownerSell = function (ownerid,storeid,petid){
    let sql = `update pet set owner_id = 0 , store_id = ? where id = ? and store_id=?`;
    let params = [ownerid,petid,storeid];
    connection.query(sql,params,function (err,result) {
        if (err){
            console.log(err);
            return
        }else{
            console.log(result)
        }
    })
}
//增加删减账目
const addAccount = function (ownerid,storeid,petid, deal_type){
    console.log(deal_type,petid,storeid,ownerid)
    let sql = `insert into account (deal_type,pet_id,seller_id,buyer_id,price,deal_time) values (?,?,?,?,500,DATE_FORMAT(NOW(),'%Y-%m-%d'));`
    let params =[deal_type,petid,storeid,ownerid];
    connection.query(sql,params,function (err,result) {
        if (err){
            console.log(err)
            return
        }else{
            console.log(result)
        }
    })
}
//查找商店
const selectStore = function (res) {
    let sql = `select * from petstore `;
    connection.query(sql,function (err,result) {
        if (err){
            console.log(err);
            return;
        }else{
            let s =  `{"count":"${result.length}","data":[`
            for (const item of result) {
                s+=`{`
                for (const key in item) {
                    s+=`"${key}":"${item[key]}",`
                }
                s = s.substring(0,s.length-1);
                s+=`},`
            }
            s = s.substring(0,s.length-1);
            s+=`]}`
            res.send(s);
        }
    })
}
// 查找商店自己的宠物
const selectMyStorePet = function (res,store_id){
    let sql = `select * from pet where store_id = ?`;
    let parmas = [store_id]
    connection.query(sql,parmas,function (err,result) {
        if (err){
            console.log(err);
            return;
        }else{
            let s =  `{"count":"${result.length}","data":[`
            for (const item of result) {
                s+=`{`
                for (const key in item) {
                    s+=`"${key}":"${item[key]}",`
                }
                s = s.substring(0,s.length-1);
                s+=`},`
            }
            s = s.substring(0,s.length-1);
            s+=`]}`
            res.send(s);
        }
    })
}
// 查找宠物主人不为0的
const selectMyOwnerPet = function (res){
    let sql = `select * from pet where owner_id <> 0 and store_id = 0`;
    connection.query(sql,function (err,result) {
        if (err){
            console.log(err);
            return;
        }else{
            let s =  `{"count":"${result.length}","data":[`
            for (const item of result) {
                s+=`{`
                for (const key in item) {
                    s+=`"${key}":"${item[key]}",`
                }
                s = s.substring(0,s.length-1);
                s+=`},`
            }
            s = s.substring(0,s.length-1);
            s+=`]}`
            res.send(s);
        }
    })
}
//寻找 商店 买家
const selectStoreOwner = function (res){
    let sql = `select * from petowner `;
    connection.query(sql,function (err,result) {
        if (err){
            console.log(err);
            return;
        }else{
            let s =  `{"count":"${result.length}","data":[`
            for (const item of result) {
                s+=`{`
                for (const key in item) {
                    s+=`"${key}":"${item[key]}",`
                }
                s = s.substring(0,s.length-1);
                s+=`},`
            }
            s = s.substring(0,s.length-1);
            s+=`]}`
            res.send(s);
        }
    })
}
module.exports= {loginUser,loginStore,selectPet,selectOwner,updatePetOwner,updatePetStore,
     ownerBuy,ownerSell,addAccount,selectStore, selectMyStorePet,selectMyOwnerPet,selectStoreOwner}